﻿using System;
using System.Linq;
using System.ComponentModel;
using System.Windows.Data;
using System.Collections.ObjectModel;

using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;

using StudentModule.Models;
using StudentModule.Services;
using ServiceAgent;
using Infrastructure;
using Infrastructure.Helpers;
using Infrastructure.Events;
using System.Windows.Input;
using System.Windows;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
using StudentModule.Views;
using ServiceAgent.StudentService;
using System.Collections.Generic;
using ServiceAgent.CatalogueService;
using StudentModule.Commands;
using Infrastructure.Account;
//using StudentModule.Events;

namespace StudentModule.ViewModels
{
    /// <summary>
    /// ViewModel for SearchSinhVienView.
    /// </summary>
    public class SearchSinhVienViewModel : NotificationObject, INavigationAware, IRegionMemberLifetime
    {
        #region Fields
        private IEventAggregator eventAggregator;

        private readonly IUnityContainer container;
        private IRegionManager regionManager;

        string query;

        ObservableCollection<DonViDTO> donViList;
        ObservableCollection<LopDTO> lopList;

        DonViDTO currentDonVi;
        LopDTO currentLop;

        bool isCapTruong = true;
        
        ObservableCollection<SinhVienBasicDTO> list = new ObservableCollection<SinhVienBasicDTO>();
        SinhVienBasicDTO currentItem;

        #endregion

        #region Constructor
        /// <summary>
        /// Default constructor.
        /// </summary>
        public SearchSinhVienViewModel(IEventAggregator eventAggregator, IRegionManager regionManager, IUnityContainer container)
        {
            this.eventAggregator = eventAggregator;
            this.regionManager = regionManager;
            this.container = container;

            this.Initialize();
        }
        #endregion

        #region Administrative Properties
        public string Query
        {
            get { return query; }
            set
            {
                query = value;
                RaisePropertyChanged(() => this.Query);
                //this.SearchCommand.RaiseCanExecuteChanged();
            }
        }

        public ObservableCollection<SinhVienBasicDTO> List
        {
            get { return list; }
            set
            {
                list = value;
                RaisePropertyChanged(() => this.List);
            }
        }

        public SinhVienBasicDTO CurrentItem
        {
            get { return currentItem; }
            set
            {
                currentItem = value;
                RaisePropertyChanged(() => this.List);
            }
        }

        public DonViDTO CurrentDonVi
        {
            get { return currentDonVi; }
            set
            {
                currentDonVi = value;
                RaisePropertyChanged(() => this.CurrentDonVi);
                this.GetLopList();
            }
        }

        public LopDTO CurrentLop
        {
            get { return currentLop; }
            set
            {
                currentLop = value;
                RaisePropertyChanged(() => this.CurrentLop);
            }
        }

        public ObservableCollection<DonViDTO> DonViList
        {
            get { return donViList; }
            set
            {
                donViList = value;
                RaisePropertyChanged(() => this.DonViList);
            }
        }

        public ObservableCollection<LopDTO> LopList
        {
            get { return lopList; }
            set
            {
                lopList = value;
                RaisePropertyChanged(() => this.LopList);
            }
        }

        public bool IsCapTruong
        {
            get { return isCapTruong; }
            set
            {
                isCapTruong = value;
                RaisePropertyChanged(() => this.IsCapTruong);
            }
        }

        public IRegionManager RegionManager
        {
            get { return regionManager; }
            set { regionManager = value; }
        }

        public IEventAggregator EventAggregator
        {
            get { return eventAggregator; }
            set { eventAggregator = value; }
        }
        #endregion

        #region Command Properties
        public ICommand EditCommand { get; private set; }
        public DelegateCommand SearchCommand { get; private set; }
        #endregion

        #region Private Methods
        private void Initialize()
        {

            this.EditCommand = container.Resolve<ShowSinhVienDetailViewCommand>(
                new ParameterOverrides 
                {
                    {"viewModel", this}
                }.OnType<ShowSinhVienDetailViewCommand>()
            );
            this.SearchCommand = new DelegateCommand(this.OnRaiseSearch);

            this.GetDonViList();
            //this.GetLopList();

            foreach (var donVi in ActiveAccount.Instance.DanhSachDonVi)
            {
                if (donVi.IsCapTruong == false)
                {
                    this.IsCapTruong = false;
                    using (var proxy = container.Resolve<CatalogueServiceAgent>())
                    {
                        this.CurrentDonVi = proxy.GetDonViByID(donVi.ID);
                    }
                }
            }
            
        }

        void GetDonViList()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    DonViList = proxy.GetDonViListByCapDonVi();
                }
            }
            catch (Exception)
            {

            }
        }

        void GetLopList()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {

                    //if (this.CurrentDonVi == null)
                    //{
                    //    LopList = proxy.GetLopList();
                    //}
                    //else
                    //{
                    //    if (this.CurrentDonVi != null)
                    //    {
                            //LopList.Clear();
                            LopList = proxy.GetLopByDonViID(this.CurrentDonVi.ID);
                    //    }
                    //}

                }
            }
            catch (Exception)
            {

            }
        }

        private void OnRaiseSearch()
        {
            try
            {
                using (var proxy = container.Resolve<StudentServiceAgent>())
                {
                    if (this.CurrentDonVi != null)
                    {
                        if (this.CurrentLop != null)
                        {
                            List = proxy.SearchSinhVienByLop(this.Query, this.CurrentLop.ID);
                        }
                        else
                        {
                            List = proxy.SearchSinhVienByDonVi(this.Query, this.CurrentDonVi.ID);
                        }
                    }
                    else
                    {
                        List = proxy.SearchSinhVien(this.Query);
                    }
                    
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        private bool CanRaiseSearch()
        {
            return !string.IsNullOrEmpty(Query);
        }
        #endregion

        #region INavigationAware Members
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            // Called to see if this view can handle the navigation request.
            // If it can, this view is activated.
            return false;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

            // Called when this view is deactivated as a result of navigation to another view.
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            //// Called to initialize this view during navigation.

            //// Retrieve any required paramaters from the navigation Uri.
            //string id = navigationContext.Parameters["ID"];
            //if (!string.IsNullOrEmpty(id))
            //{
            //    DataItem item = _dataService.GetModel().FirstOrDefault(dataItem => dataItem.Id == id);
            //}
        }
        #endregion

        #region IRegionMemberLifetime Members
        public bool KeepAlive
        {
            get { return true; }
        }
        #endregion
    }
}
